{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc19031c-bc2f-4294-95ce-3a2d9b86f44d",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "sns.set_theme(style=\"whitegrid\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c72b5394-ff5f-42b1-b083-2e42b2ffdf0f",
   "metadata": {},
   "source": [
    "The default violinplot represents a distribution two ways: a patch showing a symmetric kernel density estimate (KDE), and the quartiles / whiskers of a box plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27d578fb-1c20-4d31-b93d-b1b4a053992b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = sns.load_dataset(\"titanic\")\n",
    "sns.violinplot(x=df[\"age\"])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "e7d25589-0dc9-48ce-92f9-ab61ffbf964a",
   "metadata": {},
   "source": [
    "In a bivariate plot, one of the variables will \"group\" so that multiple violins are drawn:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b851b2c-0011-4cff-8719-11f6138c44e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"age\", y=\"class\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "6d588b32-b14b-4b33-bbd9-69b17f8212a6",
   "metadata": {},
   "source": [
    "By default, the orientation of the plot is determined by the variable types, preferring to group by a categorical variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4810c8e7-0864-496f-8e86-a6527369b9e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"class\", y=\"age\", hue=\"alive\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "402812f2-c024-4179-9fee-fed92f03deb2",
   "metadata": {},
   "source": [
    "Pass `fill=False` to draw line-art violins:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e00ce8b-5871-486b-8c55-a4f2e764aa86",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"class\", y=\"age\", hue=\"alive\", fill=False)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "8350abce-6a40-4e18-9501-7d358192471b",
   "metadata": {},
   "source": [
    "Draw \"split\" violins to take up less space, and only show the data quarties:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ae35376-5272-496c-afec-c60a3426f1bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"class\", y=\"age\", hue=\"alive\", split=True, inner=\"quart\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "90f4263f-7294-4ad5-bff4-25d7d796cb45",
   "metadata": {},
   "source": [
    "Add a small gap between the dodged violins:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26cb5b89-496d-4893-8914-ca8b6fbf97b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"class\", y=\"age\", hue=\"alive\", split=True, gap=.1, inner=\"quart\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "bbea49e0-7b08-4b25-8686-1d5404b71601",
   "metadata": {},
   "source": [
    "Starting in version 0.13.0, it is possilbe to \"split\" single violins:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba261531-a280-44e5-b8c0-bcc5a53f60bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"class\", y=\"age\", split=True, inner=\"quart\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "7c4dafa1-2747-4b43-ba4a-4c9b32778086",
   "metadata": {},
   "source": [
    "Represent every observation inside the distribution by setting `inner=\"stick\"` or `inner=\"point\"`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00b5f00e-a515-4e53-9d73-d13b045cd4c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"age\", y=\"deck\", inner=\"point\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "23c13695-cd01-4da8-bc89-2519ae445f9f",
   "metadata": {},
   "source": [
    "Normalize the width of each violin to represent the number of observations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be59f17e-824e-4a8c-a0e1-a27874a05df6",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"age\", y=\"deck\", inner=\"point\", density_norm=\"count\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "abe650fb-4d26-4bac-97f3-f451a3872cf5",
   "metadata": {},
   "source": [
    "By default, the KDE will smooth past the extremes of the observed data; set `cut=0` to prevent this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82556de0-3756-426c-a591-9af6ed6c45d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"age\", y=\"alive\", cut=0, inner=\"stick\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "abfb9e78-d524-4536-90ef-c71834b055f9",
   "metadata": {},
   "source": [
    "The `bw_adjust` parameter controls the amount of smoothing:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d17e1e3-e0f4-4d2c-ac6e-aec42ed75390",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"age\", y=\"alive\", bw_adjust=.5, inner=\"stick\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "407bc513-5b7f-418c-8ffe-ec488836586d",
   "metadata": {},
   "source": [
    "By default, the violins are drawn at fixed positions on a categorical scale, even if the grouping variable is numeric. Starting in version 0.13.0, pass the `native_scale=True` parameter to preserve the original scale on both axes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7b6d901-9a97-4716-8d24-1b30145e9c57",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(x=df[\"age\"].round(-1) + 5, y=df[\"fare\"], native_scale=True)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "790e3989-0b47-4e77-9bdb-dc757d1e938c",
   "metadata": {},
   "source": [
    "When using a categorical scale, the `formatter` parameter accepts a function that defines categories:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28a769d4-3e23-4b53-a9ef-391d5fc24201",
   "metadata": {},
   "outputs": [],
   "source": [
    "decades = lambda x: f\"{int(x)}–{int(x + 10)}\"\n",
    "sns.violinplot(x=df[\"age\"].round(-1), y=df[\"fare\"], formatter=decades)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "6f914d73-7a0c-4fbc-8432-40c4f0577857",
   "metadata": {},
   "source": [
    "By default, the \"inner\" representation scales with the `linewidth` and `linecolor` parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18cb2afd-8487-40bd-b3f2-1f83243ffa3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"age\", linewidth=1, linecolor=\"k\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "ca2ef541-c07f-4853-ba98-ce75855ba262",
   "metadata": {},
   "source": [
    "Use `inner_kws` to pass parameters directly to the inner plotting functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "934f91bc-2698-4c07-92cf-4e6039c801b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.violinplot(data=df, x=\"age\", inner_kws=dict(box_width=15, whis_width=2, color=\".8\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4aa00d3c-f016-4db8-b6b0-da4e6a327831",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "py310"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
